package com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.impl;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO.SentinelDegradeRuleDO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelDegradeRuleDTO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.constants.TableNameConstants;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.mapper.DegradeRuleMapper;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.DegradeRuleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.r2dbc.spi.ConnectionFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

import static org.springframework.data.r2dbc.query.Criteria.where;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/3 9:21
 */
@Service
@RequiredArgsConstructor
public class DegradeRuleServiceImpl implements DegradeRuleService {

    private final DegradeRuleMapper degradeRuleMapper;
    private final ConnectionFactory connectionFactory;

    private DatabaseClient client = null;

    @PostConstruct
    void init() {
        this.client = DatabaseClient.create(connectionFactory);
    }

    private SentinelDegradeRuleDO copyField(SentinelDegradeRuleDTO sentinelDegradeRuleDTO) {
        SentinelDegradeRuleDO sentinelDegradeRuleDO = new SentinelDegradeRuleDO();
        SentinelDegradeRuleDO.DegradeRule degradeRule = new SentinelDegradeRuleDO.DegradeRule();
        BeanUtils.copyProperties(sentinelDegradeRuleDTO, sentinelDegradeRuleDO, "degrade_rule");
        BeanUtils.copyProperties(sentinelDegradeRuleDTO, degradeRule);
        String degradeRuleStr = "";
        try {
            degradeRuleStr = new ObjectMapper().writeValueAsString(degradeRule);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        sentinelDegradeRuleDO.setDegradeRule(degradeRuleStr);
        return sentinelDegradeRuleDO;
    }
    @Override
    public Mono<Integer> save(SentinelDegradeRuleDTO sentinelDegradeRuleDTO) {
        SentinelDegradeRuleDO sentinelDegradeRuleDO = copyField(sentinelDegradeRuleDTO);

        return client.insert()
                .into(TableNameConstants.DEGRADERULE)
                .nullValue("id", Long.class)
                .value("application_name", sentinelDegradeRuleDO.getApplicationName())
                .value("resource", sentinelDegradeRuleDO.getResource())
                .value("degrade_rule", sentinelDegradeRuleDO.getDegradeRule())
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<Integer> deleteById(Long id) {
        return degradeRuleMapper.deleteByID(id);
    }

    @Override
    public Mono<Integer> deleteByAyApplicationName(String applicationName) {
        return degradeRuleMapper.deleteByAyApplicationName(applicationName);
    }

    @Override
    public Mono<Integer> updateById(SentinelDegradeRuleDTO sentinelDegradeRuleDTO, Long id) {
        SentinelDegradeRuleDO sentinelDegradeRuleDO = copyField(sentinelDegradeRuleDTO);
        return client.update()
                .table(SentinelDegradeRuleDO.class)
                .using(sentinelDegradeRuleDO)
                .matching(where("id").is(id))
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<SentinelDegradeRuleDTO> findById(Long id) {
        return client.select()
                .from(TableNameConstants.DEGRADERULE)
                .matching(where("id").is(id))
                .as(SentinelDegradeRuleDO.class)
                .fetch()
                .one()
                .map(SentinelDegradeRuleDO::getSentinelDegradeRuleDTO)
                .log();
    }

    @Override
    public Flux<SentinelDegradeRuleDTO> findByApplicationName(String applicationName) {
        return client.select()
                .from(TableNameConstants.DEGRADERULE)
                .matching(where("application_name").is(applicationName))
                .as(SentinelDegradeRuleDO.class)
                .fetch()
                .all()
                .map(SentinelDegradeRuleDO::getSentinelDegradeRuleDTO)
                .log();
    }
}
